package com.job.controller;

import com.job.pojo.UserInfo;
import com.job.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
@RequestMapping("Sanyecao_Login")
public class LoginController {

    @Autowired
    UserInfoService userInfoService;


    //处理邮件发送的请求
    @RequestMapping("/sendEmail")
    @ResponseBody
    public HashMap<String, Object> sendEmail(String email, HttpServletRequest request) {
        return userInfoService.sendMail(email, request);
    }

    //处理登录验证请求
    @RequestMapping("/VerifyLogin")
    @ResponseBody
    public HashMap<String, Object> VerifyLogin(UserInfo user, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String isRight = userInfoService.VerifyCodefunction(user);
        String msg = "";
        //首先验证验证码对不对
        if (isRight.equals("验证码正确")) {
            //再验证账号和密码
            msg = userInfoService.loginVerify(user);
        } else {
            msg = "验证码错误";
        }
        map.put("info", msg);
        System.out.println(msg);
        return map;
    }

    //访问login.html页面方法
    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    //处理修改密码请求
    @RequestMapping("/ChangePass")
    @ResponseBody
    public HashMap<String, Object> ChangePass(UserInfo user, HttpServletRequest request) {
        System.out.println("ChangePass被访问了");
        //先检查验证码
        String isRight = userInfoService.VerifyCodefunction(user);
        String msg = "";
        //首先验证验证码对不对
        if (isRight.equals("验证码正确")) {
            //再修改密码
            msg = userInfoService.ChangePass(user);
        } else {
            msg = "验证码错误";
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("info", msg);
        return map;
    }
    //处理退出请求
    @RequestMapping("/exit")
    @ResponseBody
    public void exit(){
        userInfoService.exit();
    }

    //返回用户名
    @RequestMapping("/getU")
    @ResponseBody
    public  HashMap<String, Object> getU(HttpServletRequest request){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("info", userInfoService.getU());
        System.out.println(userInfoService.getU());
        return map;
    }

}
